#include"Basic.hlsli"

#include "VertexHelper.hlsli"

Texture2D gAlbedoTex : register(t0);
Texture2D gNormalTex : register(t1);
Texture2D gORMTex : register(t3);


struct PixelOutput
{
    float4 albedo : SV_Target0;
    float4 normal : SV_Target1;
    float4 worldPos : SV_Target2;
    float4 aoRoughnessMetal : SV_Target3;
};

float3 CalculateNormalFromMap(float3 normalFromMap,float3 normal,float3 tangent)
{
    float3 fixedNormal = normalFromMap * 2.0f - 1.0f;
    
    float3 N = normal;
    float3 T = normalize(tangent - dot(tangent, normal) * normal);
    float3 B = cross(N, T);

    float3x3 TBN = float3x3(T, B, N);
    
    return normalize(mul(fixedNormal, TBN));
}

PixelOutput PS(VertexOut pIn) : SV_Target
{
    PixelOutput pOut;
    
    pOut.worldPos = float4(pIn.posW, 1.0f);
    
    //pOut.albedo = gAlbedoTex.Sample(g_Sam, pIn.uv);
    pOut.albedo = float4(gMaterial.kd,gId[0]);
    
    float3 norFromMap = gNormalTex.Sample(g_Sam, pIn.uv);
    
    pOut.normal = float4(pIn.norW, gId[1]); //float4(CalculateNormalFromMap(norFromMap, pIn.norW, (float3) pIn.tanW), gId[1]);
    //pOut.normal = float4(CalculateNormalFromMap(norFromMap, pIn.norW, (float3) pIn.tanW), gId[1]);
    //pOut.aoRoughnessMetal = gORMTex.Sample(g_Sam, pIn.uv);
    pOut.aoRoughnessMetal = gMaterial.ks;
    return pOut;
}

